﻿// Autore: Mattia Barrasso

using System.Linq;
using NUnit.Framework;
using RecipesManager.Data;
using TechTalk.SpecFlow;
using White.Core.UIItems.Finders;
using ListView = White.Core.UIItems.ListView;

namespace RecipesManager.Specs.Ingrediente
{
    [Binding, Scope(Feature = "ModificaIngrediente")]
    public class ModificaIngrediente_Steps : ReceipeManagerBdd
    {
        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }

        [Given(Regex = @"l'ingrediente ([a-zA-Z]+) esiste")]
        public void DatiIngredientiEsistenti(string ingrediente)
        {
            Manager.Ingredients.Add(ingrediente, MeasureType.Weight);
            Assert.That(Manager.Ingredients.Contains(ingrediente));
        }

        [Given(Regex = @"l'ingrediente ([a-zA-Z]+) non esiste")]
        public void DatiIngredientiNonEsistenti(string ingrediente)
        {
            Assert.That(!Manager.Ingredients.Contains(ingrediente));
        }

        [Given(Regex = @"esistono le categorie")]
        public void DateCategorieEsistenti(Table table)
        {
            foreach (TableRow row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    Manager.Categories.Add(row["Valori"]);
                }
                else
                {
                    Assert.Fail(@"Tabella mal formata");
                }
            }
        }

        [Given(Regex = @"esistono i negozi")]
        public void DatiNegoziEsistenti(Table table)
        {
            foreach (TableRow row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    Manager.Shops.Add(row["Valori"]);
                }
                else
                {
                    Assert.Fail(@"Tabella mal formata");
                }
            }
        }

        [Given(Regex = @"modifico l'ingrediente ([a-zA-Z]+)")]
        public void DatoModificoIngrediente(string ingrediente)
        {
            NavigateToIngredients();
            GetIgredientsUi();

            Assert.That(Manager.Ingredients.Contains(ingrediente));
            Assert.That(_ingredients.Items.Any(item => item.Text.Equals(ingrediente)));
            _ingredients.Select(ingrediente);
        }

        [Given(Regex = @"ho inserito i valori")]
        public void DatoIngredienteInserito(Table table)
        {
            GetSingleIngredientUi();
            foreach (TableRow row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    _ingredientName.SetValue(row["Valori"]);
                }
                else if (row["Attributi"].Equals("Unità"))
                {
                    switch (row["Valori"])
                    {
                        case "Peso":
                            _unitButtons[0].Click();
                            break;
                        case "Volume":
                            _unitButtons[1].Click();
                            break;
                        case "Unità":
                            _unitButtons[2].Click();
                            break;
                        default:
                            Assert.Fail("Unità");
                            break;
                    }
                }
                else if (row["Attributi"].Equals("Categoria"))
                {
                    Assert.That(Manager.Categories.Contains(row["Valori"]));
                    _categoriesBox.Click();
                    _categoriesBox.Select(row["Valori"]);
                    _addCategory.Click();
                }
                else if (row["Attributi"].Equals("Negozio"))
                {
                    Assert.That(Manager.Shops.Contains(row["Valori"]));
                    _shopsBox.Click();
                    _shopsBox.Select(row["Valori"]);
                    _addShop.Click();
                }
                else
                {
                    Assert.Fail("Table");
                }
            }
        }

        [Given(Regex = @"rimuovo la categoria ([a-zA-Z]+)")]
        public void DatoRimozioneCategoria(string categoria)
        {
            Assert.That(_selectedCategories.Items.Any(item => item.Text.Equals(categoria)));
            _selectedCategories.Select(categoria);
            _removeCategory.Click();
        }

        [Given(Regex = @"rimuovo il negozio ([a-zA-Z]+)")]
        public void DatoRimozioneNegozio(string negozio)
        {
            //TODO
            var lw = (ListView) _selectedShops;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                if (lw.Rows[i].GetElement(SearchCriteria.ByText(negozio)) != null)
                    break;
            }
            Assert.That(i != lw.Rows.Count);
            lw.Select(i);
            _removeShop.Click();
        }

        [When(Regex = @"visiono le potenziali categorie")]
        public void QuandoVisionoPotenzialiCategorie()
        {
            _categoriesBox.Click();
        }

        [When(Regex = @"visiono i potenziali negozi")]
        public void QuandoVisionoPotenzialiNegozi()
        {
            _shopsBox.Click();
        }

        [When(Regex = @"salvo")]
        public void QuandoSalvo()
        {
            _saveButton.Click();
        }

        [Then(Regex = @"l'operazione non ha successo")]
        public void AlloraInsuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count != 0);
        }

        [Then(Regex = @"l'operazione ha successo")]
        public void AlloraSuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count == 0);
        }

        [Then(Regex = @"l'ingrediente ([a-zA-Z]+) esiste")]
        public void AlloraIngredienteEsiste(string ingrediente)
        {
            Assert.That(_ingredients.Items.Any(item => item.Text.Equals(ingrediente)));
//            Assert.That(Manager.Ingredients.Contains(ingrediente));
        }

        [Then(Regex = @"l'ingrediente ([a-zA-Z]+) non esiste")]
        public void AlloraIngredienteNonEsiste(string ingrediente)
        {
//            Assert.That(!Manager.Ingredients.Contains(ingrediente));
            Assert.That(!_ingredients.Items.Any(item => item.Text.Equals(ingrediente)));
        }

        [Then(Regex = @"la lista delle categorie contiene ([0-9])+ elemento")]
        public void AlloraLaListaContieneNCategorie(int nCategorie)
        {
            Assert.That(_categoriesBox.Items.Count == nCategorie + 1);
        }

        [Then(Regex = @"la lista dei negozi contiene ([0-9])+ elemento")]
        public void AlloraLaListaContieneNNegozi(int nNegozi)
        {
            Assert.That(_shopsBox.Items.Count == nNegozi + 1);
        }
    }
}
